package com.hdaccp.foodsales.controller;

import com.hdaccp.foodsales.dao.UserPermissionDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/rolepermission")
@Scope("prototype")
public class UserPermissionController {
    @Autowired
    private UserPermissionDao userPermissionDao;
    @RequestMapping("/grantPermission")
    @ResponseBody
    public Map<String,Object>grantPermission(int roleid,String pmid){
        Map<String,Object>map =new HashMap<>();
//        System.out.println(roleid+">>"+pmid);
        try {
            String[] ss = pmid.split("-") ;
            long[] perids = new long[ss.length-1] ;
            for(int i=0;i<perids.length;i++){
                perids[i] = Long.parseLong(ss[i+1]) ;
            }
            //删除角色对应的权限
            userPermissionDao.del(roleid);
            //增加选择权限
            for (long aaaa:perids){
                userPermissionDao.save(roleid,aaaa);
            }
            map.put("msg","1") ;
        } catch (NumberFormatException e) {
            map.put("msg","2") ;
            e.printStackTrace();
        }
        return map;
    }
}
